#!/user/bin/python
# _*_ coding: UTF-8 _*_
# Filename: testTaobao.py
import random

from selenium import webdriver
import time
import pymongo
import re

client = pymongo.MongoClient('mongodb://localhost:27017')
mydb = client["jdmpresystem"]
product = mydb['Taobaoproducts']

driver = webdriver.Chrome()
# 修改浏览器部分属性，绕过检测。
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    "source": """Object.defineProperty(navigator,'webdriver',{get:()=> false})"""
})
driver.get('https://www.taobao.com/')
driver.implicitly_wait(10)  # 设置浏览器的等待（耗时） 智能化等待 加载完就不会等待
driver.maximize_window()
driver.find_element_by_xpath('//*[@id="q"]').send_keys('手机')
driver.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()
# //*[@id="fm-login-id"]//*[@id="fm-login-password"]//*[@id="login-form"]/div[4]/button
# 登录
driver.find_element_by_xpath('//*[@id="fm-login-id"]').send_keys('账号')
time.sleep(random.randint(3, 5))
driver.find_element_by_xpath('//*[@id="fm-login-password"]').send_keys('密码')
time.sleep(random.randint(3, 5))
driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()
time.sleep(30)


def drop_down():
    for x in range(1, 10, 2):
        j = x / 9
        js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
        driver.execute_script(js)
        time.sleep(0.5)


nobrand = ['守护宝旗舰店', 'applestore', 'coolpad酷派旗舰店', 'moto摩托罗拉旗舰店', '纽曼手机旗舰店', 'ktouch天语旗舰店', '联想鸿为源专卖店']

setall = ['喵速达电器官方旗舰店',
          '中国电信官方旗舰店', '联通华盛手机官方旗舰店', '中国联通官方旗舰店',
          '中国移动官方旗舰店', '能良数码官方旗舰店', '三际数码官方旗舰店', '中国移动手机官方旗舰店',
          '苏宁易购官方旗舰店']

for i in range(0, 100):
    time.sleep(random.randint(2, 3))
    drop_down()
    lis = driver.find_elements_by_xpath('//*[@id="mainsrp-itemlist"]/div/div/div[1]/div')
    price = ''
    shopname = ''
    durl = ''
    skuname = ''
    imgurl=''
    for li in lis:
        shopname = li.find_element_by_css_selector(
            ' div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-3.g-clearfix > div.shop > a > span:nth-child(2)').get_attribute(
            'innerHTML')
        isguanfan = shopname.find('官方')
        if isguanfan != -1:
            if shopname == '能良数码官方旗舰店' or shopname =='三际数码官方旗舰店':
                continue
            else:
                price = li.find_element_by_css_selector(
                    ' div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-1.g-clearfix > div.price.g_price.g_price-highlight > strong').get_attribute(
                    'innerHTML')
                price = int(price.split('.')[0])
                comment = li.find_element_by_css_selector('div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-1.g-clearfix > div.deal-cnt').text
                comment = comment.split('人')[0]
                durl = li.find_element_by_css_selector(
                    ' div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-2.title >a').get_attribute('href')
                skuname = li.find_element_by_css_selector(
                    ' div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-2.title>a').get_attribute('innerHTML')
                skuname = re.sub('<span.*?</span>', '', skuname)
                skuname = skuname.strip()
                imgurl = li.find_element_by_css_selector(' div.pic-box.J_MouseEneterLeave.J_PicBox > div > div.pic>a>img').get_attribute('src')
                phone = {
                    'price': price,
                    'comment': comment,
                    'shopname': shopname,
                    'url': durl,
                    'skuname': skuname,
                    'img':imgurl
                }
                # print(imgurl)
                x = product.insert_one(phone)
                print(x)
        else:
            flag = 1
            for bname in nobrand:
                if shopname == bname:
                    flag = 0
                    break
                else:
                    flag = 1
            if flag == 0:
                price = li.find_element_by_css_selector(
                    ' div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-1.g-clearfix > div.price.g_price.g_price-highlight > strong').get_attribute(
                    'innerHTML')
                price = int(price.split('.')[0])
                comment = li.find_element_by_css_selector(
                    'div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-1.g-clearfix > div.deal-cnt').text
                comment = comment.split('人')[0]
                durl = li.find_element_by_css_selector(
                    ' div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-2.title >a').get_attribute('href')
                skuname = li.find_element_by_css_selector(
                    ' div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-2.title>a').get_attribute('innerHTML')
                skuname = re.sub('<span.*?</span>', '', skuname)
                skuname = skuname.strip()
                imgurl = li.find_element_by_css_selector(
                    ' div.pic-box.J_MouseEneterLeave.J_PicBox > div > div.pic>a>img').get_attribute('src')
                phone = {
                    'price': price,
                    'comment': comment,
                    'shopname': shopname,
                    'url': durl,
                    'skuname': skuname,
                    'img': imgurl
                }
                # print(imgurl)
                x = product.insert_one(phone)
                print(x)
    time.sleep(1)
    if i == 99:
        break
    else:
        comfirmdel = driver.find_element_by_css_selector('#mainsrp-pager > div > div > div > ul > li.item.next > a')
        driver.execute_script("arguments[0].click();", comfirmdel)

# drop_down()
# driver.find_element_by_xpath('//*[@id="mainsrp-pager"]/div/div/div/div[2]/input').send_keys('100')
# ye = driver.find_element_by_xpath('//*[@id="mainsrp-pager"]/div/div/div/div[2]/span[3]')
# driver.execute_script("arguments[0].click();", ye)
